/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package webshop.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import webshop.data.ShoppingBasket;
import webshop.data.Person;
import webshop.data.Orderline;
import webshop.data.Product;
import webshop.data.OrderInvoice;


/**
 *
 * @author Ohm
 */
public class ShoppingBasketListServlet extends AbstractSuperServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        EntityManager em = emf.createEntityManager();
        
        Person person = null;
        Product product = null;

        try {
            PersonUtil personUtil = new PersonUtil(em, request);
            person = personUtil.isActive();
            
            if(person == null){
                out.println("You are not logged in, please login.");
            } else {
                out.println("<em>" + person.getName() + " has the following items in his basket.</em>");
                Query q = em.createNamedQuery("Orderline.selectAll");
                List<Orderline> orderlines = q.getResultList();
                out.println("<table>");
                out.println("<th>Product Id</th> ");
                out.println("<th>Name</th>");
                out.println("<th> Description</th>");
                out.println("<th> Price </th> ");
                out.println("<th> Ordered quantity</th> ");
                for (Orderline orderline : orderlines){
                        if(person.getShoppingBasketId() == orderline.getShoppingBasketId()){
                        out.println("<tr><td>"+orderline.getProduct().getProductId()+"</td><td> "+
                        orderline.getProduct().getName()+"</td><td> "+
                        orderline.getProduct().getDescription()+"</td><td> "+
                        orderline.getProduct().getPrice()+"</td><td> "+
                        orderline.getQuantity()+"</td></tr> ");
                        }
                    }
                out.println("</table>");
                }
            
         

        } finally { 
            out.close();
        }
    } 
}